﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using ProjectA.Utils;
using System.ComponentModel;

namespace ProjectA.Controls
{
    public class cButton : Button
    {

        public Image Image1 { get; set; }
        public Image Image2 { get; set; }
        public Image Image3 { get; set; }
        public Image Image4 { get; set; }

        public cButton(Point pPoint, Size sSize, EventHandler ehClick, Bitmap Img1, Bitmap Img2, Bitmap Img3, Bitmap Img4)
        {
            
            Location = pPoint;
            Size = sSize;
            
            Image1 = ((System.Drawing.Image)(Img1));
            Image2 = ((System.Drawing.Image)(Img2));
            Image3 = ((System.Drawing.Image)(Img3));
            Image4 = ((System.Drawing.Image)(Img4));
            
            Design();
            Eventos();
            if (ehClick != null) 
                Click += ehClick;
        }

        private void Design()
        {
            
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            BackColor = Color.Transparent;
            UseVisualStyleBackColor = true;
            FlatAppearance.MouseDownBackColor = Color.Transparent;
            FlatAppearance.MouseOverBackColor = Color.Transparent;
            FlatAppearance.BorderSize = 0;
            FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            Anchor = System.Windows.Forms.AnchorStyles.None;
            BackgroundImage = Image1;
        }

        private void Eventos()
        {
            MouseDown += new MouseEventHandler(mouseDown);
            MouseEnter += new System.EventHandler(mouseEnter);
            MouseLeave += new System.EventHandler(mouseLeave);
            MouseUp += new MouseEventHandler(mouseUp);
            EnabledChanged += new System.EventHandler(this.ButtonEnabledChanged);
        }

        void ButtonEnabledChanged(object sender, EventArgs e)
        {
            Button b = sender as Button;
            if (b.Enabled)
                b.BackgroundImage = Image1;
            else if(Image4 != null && !b.Enabled)
                b.BackgroundImage = Image4;
            else if (Image4 == null && !b.Enabled)
            {
                b.BackgroundImage = null;
                b.BackColor = Color.Gray;
            }
        }

        private void mouseDown(object sender, MouseEventArgs e)
        {
            this.BackgroundImage = Image3;
        }
        private void mouseEnter(object sender, EventArgs e)
        {
            this.BackgroundImage = Image2;
        }
        private void mouseLeave(object sender, EventArgs e)
        {
            this.BackgroundImage = Image1;
        }
        private void mouseUp(object sender, MouseEventArgs e)
        {
            this.BackgroundImage = Image2;
        }

        [Browsable(false)]
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;
                return cp;
            }
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            // Do Nothing
        }
    }
}
